package com.example.lib_adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup
import androidx.recyclerview.widget.RecyclerView
import kotlin.properties.Delegates

abstract class BaseQuickAdapter<T, U>(layout:Int):RecyclerView.Adapter<BaseViewHolder> (){
    var layout by Delegates.notNull<Int>()
    var data=ArrayList<T>()

    init {
        this.layout=layout
    }

    @JvmField
    var onItemClickListener:OnItemClickListener?=null
    @JvmField
    var onItemLongClickListener:OnItemLongClickListener?=null
    @JvmField
    var onItemChildClickListener:OnItemChildClickListener?=null

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        val layoutManager = recyclerView.layoutManager
        if(layoutManager is GridLayoutManager){
            val manager = layoutManager as GridLayoutManager
            manager.spanSizeLookup=object :SpanSizeLookup(){
                override fun getSpanSize(position: Int): Int {
                    return 1
                }

            }
        }
    }

    //添加数据
    fun addData(entity: T){
        data.add(entity)
        notifyItemInserted(data.size-1)
    }

    fun addAll(list: List<T>){
        data.clear()
        data.addAll(list)
        notifyDataSetChanged()
    }

    fun getData():List<T>{
        return data
    }

    fun removeData(position: Int){
        data.removeAt(position)
        notifyItemRemoved(position)
    }


    /*abstract fun getMutipleSpanSize(position: Int, recyclerView: RecyclerView): Int*/

    //存放所有需要点击事件的控件对象
    var ids= mutableListOf<Int>()
    fun addItemChildClick(vararg id: Int){
        id.forEach {
            ids.add(it)
        }
    }
    /*private lateinit var viewHolder:BaseViewHolder
    var viewHolder2:BaseViewHolder
        get() {
           return viewHolder
        }
        set(value) {}*/


    //创建ViewHolder
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        val view= LayoutInflater.from(parent.context).inflate(layout, parent, false)
//        var view=createItemView(viewType,parent)
        var viewHolder = BaseViewHolder(view)
        if(onItemClickListener!=null){
            viewHolder.itemView.setOnClickListener {
                onItemClickListener!!.onItemClick(viewHolder.adapterPosition)
            }
        }
        if(onItemLongClickListener!=null){
            viewHolder.itemView.setOnLongClickListener {
                onItemLongClickListener!!.onItemLongClick(viewHolder.adapterPosition)
            }
        }
        if(onItemChildClickListener!=null){
            ids.forEachIndexed {index,id->
                viewHolder.getView<View>(id).setOnClickListener {
                    onItemChildClickListener!!.onItemChildClick(this,id,viewHolder.adapterPosition)
                }
            }
        }

        return viewHolder
    }


    @JvmOverloads
    /*fun getViewHolder():BaseViewHolder{
        return viewHolder
    }*/


    fun setOnItemClickListener(onItemClickListener: OnItemClickListener){
        this.onItemClickListener=onItemClickListener
    }

    fun setOnItemLongClickListener(onItemLongClickListener: OnItemLongClickListener){
        this.onItemLongClickListener=onItemLongClickListener
    }

    fun setOnItemChildClickListaner(onItemChildClickListener: OnItemChildClickListener){
        this.onItemChildClickListener=onItemChildClickListener
    }


//    abstract fun createItemView(viewType: Int, parent: ViewGroup): View

    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        val t = data.get(position) as T
        convert(holder,t)
    }

    abstract fun convert(holder: BaseViewHolder, t: T)

//    abstract override fun getItemViewType(position: Int): Int

    override fun getItemCount(): Int {
        return data.size
    }

    interface OnItemClickListener{
        fun onItemClick(position: Int)
    }

    interface OnItemLongClickListener{
        fun onItemLongClick(position: Int):Boolean
    }

    interface OnItemChildClickListener{
        fun onItemChildClick(adapter: BaseQuickAdapter<*,*>,id:Int,position: Int)
    }
}